package com.base.study.activity;

import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.SubStringUtil;
import com.base.study.R;
import com.base.study.adapter.ForTyoeAdapter;
import com.base.study.bean.FromStudyTypeistBean;
import com.base.study.databinding.ActivitySubjectBinding;
import com.base.study.presenter.SubjectFromTypePresenter;
import com.base.study.view.SubjectFormTypeView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zw on 2020/3/28.
 * 从学习分类跳转过来的
 */
@Route(path = "/subject/formsubject")
public class FromSubjectTypeActivity extends CBaseBarMvpActivity<SubjectFromTypePresenter> implements SubjectFormTypeView {
    private ActivitySubjectBinding mBinding;
    private List<FromStudyTypeistBean> mData=new ArrayList<>();
    //private BaseAdapter<FromStudyTypeistBean> subjectListBeanBaseAdapter;
    private int page = 1;
    @Autowired
    public String id;
    @Autowired
    public String firstCategoryName;
    @Autowired
    public String secondCategoryName;
    private SearchPopWindow mSearchPopWindow;
    private ForTyoeAdapter forTyoeAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_subject;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivitySubjectBinding) mRootBinding;
        mPresenter = new SubjectFromTypePresenter(this,this);
        mPresenter.start();
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        setBackNavigation();
        setBarTitle(firstCategoryName);
        if (mSearchPopWindow ==null){
            mSearchPopWindow = new SearchPopWindow(mContext,FromSubjectTypeActivity.this,id,secondCategoryName);
        }
        setRigImg(R.mipmap.ic_main_search, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSearchPopWindow !=null){
                    mSearchPopWindow.setHint("搜索课程");
                    mSearchPopWindow.showAsDropDown(mBinding.rvSubjectList);
                }

            }
        });

    }

    @Override
    protected void start() {
        mBinding.smartRefreshLayout.autoRefresh();
//        subjectListBeanBaseAdapter = new BaseAdapter<FromStudyTypeistBean>(mData, mContext,true) {
//            @Override
//            public void convert(BaseHolder holder, final FromStudyTypeistBean subjectListBean, boolean isHeader, boolean isBottom) {
//                TextView title = holder.getView(R.id.title);
//                TextView des = holder.getView(R.id.des);
//                ImageView item_image = holder.getView(R.id.item_image);
//                title.setText(SubStringUtil.subStringCN(subjectListBean.getName(),30));
//                des.setText(SubStringUtil.subStringCN(subjectListBean.getName(),30));
//                GlideUtil.load(mContext,subjectListBean.getCover(),item_image);
//            }
//        };
//
//        subjectListBeanBaseAdapter.putNormalType(R.layout.item_from_type);
//        subjectListBeanBaseAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
//                ARouter.getInstance().build("/curriculum/detail")
//                        .withString("id", mData.get(dataPosition).getId())
//                        .withString("url",  mData.get(dataPosition).getCover())
//                        .navigation();
//            }
//        });


        forTyoeAdapter=new ForTyoeAdapter(mContext,FromSubjectTypeActivity.this,mData,secondCategoryName,id);
        forTyoeAdapter.isHead(0);

        mBinding.rvSubjectList.setAdapter(forTyoeAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rvSubjectList.setLayoutManager(linearLayoutManager);
        mBinding.rvSubjectList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 10, 10, 10));

        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getList(page, 10,id,"");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getList(page, 10,id,"");
            }
        });

    }


    @Override
    public void returnNoData() {
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnList(List<FromStudyTypeistBean> bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(bean);
        if (forTyoeAdapter !=null){
            forTyoeAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnMoreList(List<FromStudyTypeistBean> bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(bean);
        if (forTyoeAdapter !=null){
            forTyoeAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }
}
